var t = getApp(),
  e = t.requirejs("core"),
  n = t.requirejs("jquery");
const chooseLocation = requirePlugin('chooseLocation');
Component({
  data: {
    selectedColumn: null,
    selectedColumnDT: null,
    selectedColumnDate: null,
    selectedColumnDateTimeStamp: null,
    // 当前日期
    currentDate: new Date().getTime(),
    // 最小日期
    minDate: new Date(1990, 0, 1).getTime(),
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      } else if (type === 'month') {
        return `${value}月`;
      }
      return value;
    },
    columns: ['杭州', '宁波', '温州', '嘉兴', '湖州'],
    doorTime: [],
    popupShow: false,
    popupShowDT: false,
    popupShowDate: false,
    postData: {},
    picsList: [
      [], [], [], [], [], []
    ],

  },

  behaviors: ['wx://component-export'],
  export() {
    return { postData: this.data.postData }
  },
  properties: {
    "price": { // 属性名
      type: String,
      value: '100'
    },
    "postData": { // 属性名
      type: Object,
      value: {}
    },
    "picsList": { // 属性名
      type: Array,
      value: []
    },
    "sitesShow": {
      type: Boolean,
      value: true,
    },
    "picShowCondition": {
      type: Object,
      value: {}
    }
  },
  attached() {
    this.setTime();
  },

  detached() {
    // 页面卸载时设置插件选点数据为null，防止再次进入页面，geLocation返回的是上次选点结果
    chooseLocation.setLocation(null);
  },

  pageLifetimes: {
    show: function () {
      const location = chooseLocation.getLocation() == null ? (this.data.postData.location == null ? null : this.data.postData.location) : chooseLocation.getLocation().name;
      const address = chooseLocation.getLocation() == null ? (this.data.postData.address == null ? null : this.data.postData.address) : chooseLocation.getLocation().address;
      const serviceaddress = location + '(' + address + ')';
      var r = this.data.postData;
      r['serviceaddress'] = serviceaddress;
      r['location'] = location;
      this.setData({
        postData: r

      })
    },

  },
  methods: {
    // 服务选择
    showPopup() {
      this.setData({ popupShow: true });
    },

    onInput(event) {
      this.setData({
        currentDate: event.detail,
      });
    },
    onClose() {
      this.setData({ popupShow: false });
    },

    onConfirm(event) {
      const { value } = event.detail;
      console.log('the value is ', value);

      this.setData({
        selectedColumn: value,
        popupShow: false
      })
    },

    // 车座位选中
    onSeatChange(event) {
      var r = this.data.postData;
      r['carseats'] = event.detail;
      this.setData({
        postData: r

      })
    },
    // 上门时间
    showPopupDT() {
      this.setData({ popupShowDT: true });
    },

    onCloseDT() {
      this.setData({ popupShowDT: false });
    },

    onConfirmDT(event) {
      const { value } = event.detail;
      var r = this.data.postData;
      r['servicetime'] = value[0] + " " + value[1];
      this.setData({
        selectedColumnDT: value[0] + " " + value[1],
        postData: r,
        popupShowDT: false
      })
    },

    // 注册日期
    showPopupDate() {
      this.setData({ popupShowDate: true });
    },

    onCloseDate() {
      this.setData({ popupShowDate: false });
    },

    onConfirmDate(value) {
      let date = new Date(value.detail);
      let selectedColumnDate = date.getFullYear() + "年" + (date.getMonth() + 1) + "月" + date.getDate() + "日";
      var r = this.data.postData;
      r['registertime'] = selectedColumnDate;
      this.setData({
        selectedColumnDate,
        selectedColumnDateTimeStamp: value.detail,
        postData: r,
        popupShowDate: false
      })
    },

    // 地图跳转
    navigateQQMap() {
      const key = 'Y7TBZ-7N3W4-QKPUZ-DBMS2-646GV-QXBPT'; //使用在腾讯位置服务申请的key
      const referer = '直享惠'; //调用插件的app的名称
      const category = '生活服务,娱乐休闲';
      const scale = 18;
      wx.navigateTo({
        url: 'plugin://chooseLocation/index?key=' + key + '&referer=' + referer + '&category=' + category + '&scale=' + scale
      });

    },

    // 文件上传
    chooseimg(event) {

      var that = this, i = e.pdata(event).type, img = e.pdata(event).img;
      if (img && img.length > 0) return;
      const index = event.target.dataset.idx;
      wx.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success(res) {
          // tempFilePath可以作为img标签的src属性显示图片
          const tempFilePaths = res.tempFilePaths

          //fileList.push({url: tempFilePaths });
          console.log(index, tempFilePaths);
          var r = that.data.postData;
          r[i] = tempFilePaths[0];
          that.setData({
            postData: r,
            ['picsList[' + index + ']']: [{ url: tempFilePaths[0] }]
          })
        }
      })

    },

    // 删除图片
    afterPicDel(event) {
      var that = this, i = e.pdata(event).type;
      var r = that.data.postData;
      r[i] = null;
      console.log('删除图片的序号为', event.target.dataset.idx);
      let index = event.target.dataset.idx;
      that.setData({
        postData: r,
        ['picsList[' + index + ']']: null
      });

    },

    // 设置日期，时间
    setTime() {
      // 存储当前年月日的字符表示
      let dateArr = [];
      // 存放8点至16点的时间表示
      let timeArr = [];
      let currentDate = new Date();
      for (let i = 0; i < 6; i++) {
        let newDate = new Date(currentDate.getTime() + i * 1000 * 60 * 60 * 24);
        let year = newDate.getFullYear();
        let month = (parseInt(newDate.getMonth() + 1) > 9 ? parseInt(newDate.getMonth() + 1) : '0' + parseInt(newDate.getMonth() + 1));
        let day = (newDate.getDate() > 9 ? newDate.getDate() : '0' + newDate.getDate());
        dateArr.push(`${year}-${month}-${day}`);
      }
      for (let i = 8; i < 17; i++) {
        let timeItem = "";
        if (i < 10)
          timeItem += "0";
        timeItem = timeItem + i + ":00";
        timeArr.push(timeItem);
      }
      let doorTime = [{
        values: dateArr,
      }, {
        values: timeArr,
      }];

      this.setData({
        doorTime
      });
    },
    onChange: function (t) {
      var a = t.detail.value, i = e.pdata(t).type, r = this.data.postData;

      r[i] = n.trim(a), this.setData({
        postData: r
      });
      console.log(r)
    },
  },

})